package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.service.IClientCrmRemindService;
import com.mdd.admin.validate.ClientCrmRemindCreateValidate;
import com.mdd.admin.validate.ClientCrmRemindSearchValidate;
import com.mdd.admin.validate.ClientCrmRemindUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.ClientCrmRemindDetailVo;
import com.mdd.admin.vo.ClientCrmRemindListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ClientCrmRemind;
import com.mdd.common.mapper.ClientCrmRemindMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 提醒实现类
 * @author LikeAdmin
 */
@Service
public class ClientCrmRemindServiceImpl implements IClientCrmRemindService {
        
    @Resource
    ClientCrmRemindMapper clientCrmRemindMapper;

    /**
     * 提醒列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ClientCrmRemindListedVo>
     */
    @Override
    public PageResult<ClientCrmRemindListedVo> list(PageValidate pageValidate, ClientCrmRemindSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ClientCrmRemind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        clientCrmRemindMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:updateUserId@update_user_id:int",
            "like:updateUserName@update_user_name:str",
        });

        IPage<ClientCrmRemind> iPage = clientCrmRemindMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ClientCrmRemindListedVo> list = new LinkedList<>();
        for(ClientCrmRemind item : iPage.getRecords()) {
            ClientCrmRemindListedVo vo = new ClientCrmRemindListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 提醒详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return ClientCrmRemind
     */
    @Override
    public ClientCrmRemindDetailVo detail(Integer id) {
        ClientCrmRemind model = clientCrmRemindMapper.selectOne(
                new QueryWrapper<ClientCrmRemind>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ClientCrmRemindDetailVo vo = new ClientCrmRemindDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 提醒新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ClientCrmRemindCreateValidate createValidate) {
        ClientCrmRemind model = new ClientCrmRemind();
        model.setRemindTime(createValidate.getRemindTime());
        model.setRemindContent(createValidate.getRemindContent());
        model.setRemindEndTime(createValidate.getRemindEndTime());

        // 设置 操作人信息 以及 创建时间
        model.setUserId(LikeAdminThreadLocal.getAdminId());
        model.setUserName(LikeAdminThreadLocal.get("username").toString());
        model.setCreateTime(TimeUtils.timestampToDate(String.valueOf(System.currentTimeMillis() / 1000)));
        model.setUpdateTime(TimeUtils.timestampToDate(String.valueOf(System.currentTimeMillis() / 1000)));
        model.setIsDelete(0);

        clientCrmRemindMapper.insert(model);
    }

    /**
     * 提醒编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(ClientCrmRemindUpdateValidate updateValidate) {
        ClientCrmRemind model = clientCrmRemindMapper.selectOne(
                new QueryWrapper<ClientCrmRemind>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setRemindTime(updateValidate.getRemindTime());
        model.setRemindContent(updateValidate.getRemindContent());
        model.setRemindEndTime(updateValidate.getRemindEndTime());

        // 设置 操作人信息 以及 更新时间
        model.setUpdateUserId(LikeAdminThreadLocal.getAdminId());
        model.setUpdateUserName(LikeAdminThreadLocal.get("username").toString());
        model.setUpdateTime(TimeUtils.timestampToDate(String.valueOf(System.currentTimeMillis() / 1000)));

        clientCrmRemindMapper.updateById(model);
    }

    /**
     * 提醒删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ClientCrmRemind model = clientCrmRemindMapper.selectOne(
                new QueryWrapper<ClientCrmRemind>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        clientCrmRemindMapper.updateById(model);
    }

}
